package controllers.manage.room;

import controllers.BaseController;
import controllers.auth.SkipAuth;
import enums.GoodsType;
import goods.Goods;
import models.venue.policy.Policy;
import models.venue.room.Room;
import models.venue.room.RoomVO;
import play.mvc.Controller;
import play.mvc.With;
import utils.DateUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by hanbin on 17/4/10.
 */
@With(BaseController.class)
public class RoomBespeak extends Controller {

    @SkipAuth
    public static void index(long id) {
        Room room = Room.findById(id);
        List<RoomVO> coachVOList = new ArrayList<>();
        // 获取当天  2017-10-01
        String todayString = DateUtil.dateToString(new Date() , "yyyy-MM-dd");

        // 年月日 时分秒  上课时间
        Date beginAt = DateUtil.stringToDate(todayString + " 00:00:00" , "yyyy-MM-dd HH:mm:ss");

        // 单次时长60分钟
        Integer timeLength = 60;

        Integer size = 24*60/timeLength;

        for(int i =0 ; i < size; i++) {
            RoomVO roomVO = new RoomVO();
            // 下课时间
            Date endAt = DateUtil.getAfterMinutes(beginAt , timeLength);
            roomVO.beginAt = beginAt;
            roomVO.endAt = endAt;
            String endStr = DateUtil.dateToString(roomVO.endAt , "HH:mm");
            if("00:00".equals(endStr)) endStr = "24:00";
            Policy policy = Policy.findByRoomAndBeginTimeAndEndTime(room , DateUtil.dateToString(roomVO.beginAt , "HH:mm") , endStr);
            roomVO.price = policy == null ? BigDecimal.ZERO : policy.price;
            roomVO.goodsId = Goods.findOrCreateTypeTimeGoodsForRoom(GoodsType.SPORTS_ROOMS , room, roomVO.beginAt , roomVO.endAt , roomVO.price).id;
            roomVO.orderBy = i;
            coachVOList.add(roomVO);

            beginAt = roomVO.endAt;
        }

        render(room , coachVOList);
    }
}
